package com.xiaohai.demo.touting;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 发布订阅模式，
 * 消费者(短信)
 */
public class ConsumerForSMS {
//    private static  String QUEUE_INFO_EMAIL ="queue_info_emain";
    private static  String QUEUE_INFO_SMS ="queue_info_sms";
    private static  String EXCHANGE_FANOUT_INFO ="exchange_fanout_info2";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");

        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();
        //声明交换机,(交换机名称，交换机模式)
        channel.exchangeDeclare(EXCHANGE_FANOUT_INFO, BuiltinExchangeType.DIRECT);
        //声明队列,队列名称，是否持久化，是否独占此队列，参数
        channel.queueDeclare(QUEUE_INFO_SMS,true,false,false,null);
        //绑定队列 和交换机
        channel.queueBind(QUEUE_INFO_SMS,EXCHANGE_FANOUT_INFO,QUEUE_INFO_SMS);

        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("短信服务器收到消息："+new String(body,"utf-8"));

//                super.handleDelivery(consumerTag, envelope, properties, body);
            }
        };
        channel.basicConsume(QUEUE_INFO_SMS,true,consumer);

    }
}
